package com.demo.rxjavademo.http;

import com.demo.rxjavademo.mode.BeforeNewsInfo;
import com.demo.rxjavademo.mode.LastNewsInfo;
import com.demo.rxjavademo.mode.SplashInfo;

import org.reactivestreams.Subscriber;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;


/**
 * Created by Administrator on 2016/12/19.
 */

public class Http {
    private static Http mHttp = null;
    private static Retrofit mRetrofit = null;
    private static ApiStores mApiStores = null;

    public static Http getInstance() {
        if (mHttp == null) {
            mHttp = new Http();
            mRetrofit = ApiClient.retrofit();
            mApiStores = mRetrofit.create(ApiStores.class);
        }
        return mHttp;
    }

    public static void getSplash(Subscriber<SplashInfo> splashSubscriber, String resolution) {

        Flowable<SplashInfo> flowable = mApiStores.getSplash(resolution);
        toSubscribe(flowable, splashSubscriber);
    }
    public static void getLastNews(Subscriber<LastNewsInfo> lastNewsInfoSubscriber,String date){
        Flowable<LastNewsInfo> lastNewsFlowable=mApiStores.getLastNews(date);
        toSubscribe(lastNewsFlowable,lastNewsInfoSubscriber);
    }

    public static void getBefore(Subscriber<BeforeNewsInfo> beforeNewsInfoSubscriber,String date){
        Flowable<BeforeNewsInfo> beforeNewsFlowable=mApiStores.getBeforeNews(date);
        toSubscribe(beforeNewsFlowable,beforeNewsInfoSubscriber);
    }
    private static void toSubscribe(Flowable flowable, Subscriber splashSubscriber) {
        flowable.subscribeOn(Schedulers.io())
//                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(splashSubscriber);
    }
}
